# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

import requests
from lxml import etree
from copy import deepcopy
from setting import *
import time
from untils.requestsTools import requestsTools
import os, re, json

class CitroenAll:
    def __init__(self, brand="citroen"):
        self.brand=brand
        self.cookies = {}
        self.basePath = os.getcwd() + os.sep + '..' + os.sep + 'citroenHtml' + os.sep + brand
        if not os.path.exists(self.basePath):
            os.makedirs(self.basePath)
        self.imgBasePath = os.getcwd() + os.sep + '..' + os.sep + 'citroenImg' + os.sep + brand
        if not os.path.exists(self.imgBasePath):
            os.makedirs(self.imgBasePath)
        self.resTools = requestsTools(basePath=self.basePath)

        self._suffix = "AC" if brand=="citroen" else "AP"

    def updateCookies(self, setCookies):
        for k in setCookies:
            self.cookies[k] = setCookies[k]

    def login1(self):
        url = 'http://service.citroen.com/pages/index.jsp'
        headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        response = requests.get(url=url, headers=headers, timeout=30)
        setCookies = response.cookies.get_dict()
        self.updateCookies(setCookies)
        index_html = etree.HTML(response.content.decode("utf8"))
        window_value = index_html.xpath('//input[@name="window"]/@value')[0]
        return window_value


    def login2(self):
        url = 'http://service.citroen.com/do/changerParametres'
        headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://service.citroen.com/pages/index.jsp",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        self.updateCookies({"CodeLanguePaysOI": "zh_CN", "Enabled": "true"})
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=30)


    def login3(self, window_value, username, password):
        url = 'http://service.citroen.com/do/login'
        headers = {
            "Host": "service.citroen.com",
            "Cache-Control": "max-age=0",
            "Origin": "http://service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "Content-Type": "application/x-www-form-urlencoded",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://service.citroen.com/do/changerParametres?",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        postData = {
            "userid": username,
            "password": password,
            "window": window_value,
        }
        response = requests.post(url=url, headers=headers, data=postData, cookies=self.cookies, timeout=30, allow_redirects=False)

    def login4(self):
        url = 'http://service.citroen.com/do/newApvprStart'
        headers = {
            "Host": "service.citroen.com",
            "Cache-Control": "max-age=0",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://service.citroen.com/do/changerParametres?",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=30, allow_redirects=False)

    def login5(self):
        url = 'http://service.citroen.com/do/changerParametres'
        headers = {
            "Host": "service.citroen.com",
            "Cache-Control": "max-age=0",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://service.citroen.com/do/changerParametres?",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=30, allow_redirects=False)
        setCookies = response.cookies.get_dict()
        self.updateCookies(setCookies)

    def login6(self):
        url = 'http://service.citroen.com/docapvDS/login.do?jbnRedirect=true'
        headers = {
            "Host": "service.citroen.com",
            "Cache-Control": "max-age=0",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://service.citroen.com/do/changerParametres?",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        del self.cookies["Enabled"]
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=30, allow_redirects=False)
        return deepcopy(self.cookies)

    def login(self, username, password):
        window_value = self.login1()
        self.login2()
        self.login3(window_value, username, password)
        self.login4()
        self.login5()
        cookies = self.login6()
        return cookies

    def get_carList(self):
        if self.brand=="citroen":
            url = 'http://service.citroen.com/docprAC/'
            postData = {
                "tabControlID": "5",
                "jbnContext": "true",
            }
        else:
            url = 'http://service.citroen.com/docprAP/'
            postData = {
                "tabControlID": "15",
                "jbnContext": "true",
            }
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Cache-Control": "max-age=0",
            "Origin": "http://service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "Content-Type": "application/x-www-form-urlencoded",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "http://service.citroen.com/docapvDS/login.do?jbnRedirect=true",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        self.resTools.updateCookies({"_pk_ses.66.196e": "*", "_pk_id.66.196e": "8d0d215f40659ee1.1551407579.1.1551407579.1551407579."})

        response = self.resTools.parseUrl(url=url, method="POST", postData=postData, fileName=self.brand+'CarsList')
        carsListHtml = etree.HTML(response.get("content"))
        carsNameList = carsListHtml.xpath(
            '//table[@class="vehiculeSelection"]/tr[2]//a[contains(@onclick, "onVeh")]/@title')
        carsCodeInfo = carsListHtml.xpath(
            '//table[@class="vehiculeSelection"]/tr[2]//a[contains(@onclick, "onVeh")]/@onclick')
        carList = []
        for index in range(len(carsNameList)):
            _dict = {}
            _dict["name"] = carsNameList[index]
            codeInfo = carsCodeInfo[index]
            cid = codeInfo.split(';')[0].replace("onVehComClick(", "").replace(")", "").replace("'", "").replace(",",
                                                                                                                 "_")
            _dict["cid"] = cid
            print(_dict)
            carList.append(_dict)
        return carList

    def ajaxRequestGetVehCom(self, vehCom, cat, lcdv=False):
        url = 'http://service.citroen.com/docpr{}/ajaxRequestGetVehCom.do'.format(self._suffix)
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
            "X-Prototype-Version": "1.6.0.1",
            "Origin": "http://service.citroen.com",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Referer": "http://service.citroen.com/docpr{}/".format(self._suffix),
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        }
        postData = {
            "vehCom": vehCom,
            "cat": cat,
            "lcdv": "B0D=&B0F=" if lcdv else "",
            "_": ""
        }
        htmlFile = 'ajaxRequestGetVehCom'+self.cid
        response = self.resTools.parseUrl(url=url, method="POST", postData=postData, fileName=htmlFile+'2' if lcdv else htmlFile+'1')

    def get_carIndex(self, vehCom, cat, isInit=False):
        # 选定车型后的首页
        url = 'http://service.citroen.com/docpr{}/vehCom.do?cat={}&vehCom={}&lcdv=B0D%3D%26B0F%3D'.format(self._suffix, cat, vehCom)
        referer = "http://service.citroen.com/docpr{}/initNav.do".format(self._suffix) if isInit else "http://service.citroen.com/docpr{}/".format(self._suffix)
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": referer,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        responseInfo = self.resTools.parseUrl(url=url, fileName=self.cid)
        groupInfoList = self.parse_groupInfo(responseInfo.get("content"))
        menuList = etree.HTML(responseInfo.get("content")).xpath('//ul[@class="aside"]/li[2]/ul/li[position()<5]/@id')
        menuList = list(map(lambda x: x.replace("menu_", ""), menuList))

        # 附属的两个请求
        url1 = 'http://service.citroen.com/docpr{}/affichePanier.do'.format(self._suffix)
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": url,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        _ = self.resTools.parseUrl(url=url1, fileName='affichePanier'+self.cid)

        url2 = 'http://service.citroen.com/docpr{}/panierTemp.do?typeActionPanierTemp=3'.format(self._suffix)
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
            "X-Prototype-Version": "1.6.0.1",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Referer": url,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        _ = self.resTools.parseUrl(url=url2, fileName='panierTemp'+self.cid)

        return url, menuList, groupInfoList

    def new_search(self, referer):
        print("新的查询")
        url = 'http://service.citroen.com/docpr{}/initNav.do'.format(self._suffix)
        headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": referer,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        response = requests.get(url=url, headers=headers, cookies=self.cookies, timeout=30)

    def get_groupInfoList(self, cid, menu, referer):
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": referer,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        url = 'http://service.citroen.com/docpr{}/fonction.do'.format(self._suffix)
        params = "?{}={}&{}={}&{}={}".format("idFction", menu, "topGrisageActif", "true", "tabTriEtAffich", "1")
        url = url + params
        responseInfo = self.resTools.parseUrl(url=url, fileName=cid+menu)
        groupInfoList = self.parse_groupInfo(responseInfo.get("content"))

        # 附属的两个请求
        url1 = 'http://service.citroen.com/docpr{}/affichePanier.do'.format(self._suffix)
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": url,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        _ = self.resTools.parseUrl(url=url1, fileName='affichePanier' + self.cid+ menu)

        url2 = 'http://service.citroen.com/docpr{}/panierTemp.do?typeActionPanierTemp=3'.format(self._suffix)
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
            "X-Prototype-Version": "1.6.0.1",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Referer": url,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
        }
        _ = self.resTools.parseUrl(url=url2, fileName='panierTemp' + self.cid + menu)

        return url, groupInfoList

    def parse_groupInfo(self, content):
        jsonstr = re.findall('setFctNiv1Select\((.*?)\)', content)[0]
        _json = json.loads(jsonstr)
        fctionsDependantes = _json.get("fctionsDependantes")
        groupList = []
        for info in fctionsDependantes:
            groupDict = {}
            groupDict["groupName"] = info.get("libelleFct")
            groupDict["groupId"] = info.get("id")
            groupDict["groupImg"] = info.get("imageFct")
            subgroupList = info.get("fctionsDependantes")
            subList = []
            for subgroupInfo in subgroupList:
                subDict = {}
                subDict["subName"] = subgroupInfo.get("libelleFct")
                subDict["subId"] = subgroupInfo.get("id")
                subDict["subImg"] = subgroupInfo.get("imageFct")
                subDict["grisee"] = subgroupInfo.get("grisee")
                subList.append(subDict)
            groupDict["subList"] = subList
            groupList.append(groupDict)
        return groupList


    def parseStrucInfo(self, content):
        _json = json.loads(content)
        strucList = []
        listeDesDocuments = _json.get("listDocPourAffichage", {}).get("listeDesDocuments", [])
        for info in listeDesDocuments:
            strucDict= {}
            strucDict["strucName"] = info.get("titre")
            strucDict["strucModel"] = info.get("applicabilite")
            strucDict["applicabiliteNonTraduite"] = info.get("applicabiliteNonTraduite")
            strucDict["strucId"] = info.get("reference")
            strucDict["refaff"] = info.get("referenceAffichable")
            strucList.append(strucDict)
        return strucList

    def get_strucInfo(self, cid, groupId, subId, referer):
        url1 = 'http://service.citroen.com/docpr{}/AjaxTabRecapDoc.do'.format(self._suffix)
        formData = {
            "idFct": subId,
            "idFctParent": groupId,
            "_": "",
        }
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
            "X-Prototype-Version": "1.6.0.1",
            "Origin": "http://service.citroen.com",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Referer": referer,
        }
        _ = self.resTools.parseUrl(url=url1, method="POST", postData=formData, fileName='AjaxTabRecapDoc'+cid+groupId+subId)

        url = 'http://service.citroen.com/docpr{}/AjaxListeDoc.do'.format(self._suffix)
        formData = {
            "typDoc": "",
            "_": ""
        }
        responseInfo = self.resTools.parseUrl(url=url, method="POST", postData=formData, fileName='AjaxListeDoc'+cid+groupId+subId)
        strucInfoList = self.parseStrucInfo(responseInfo.get("content"))
        return strucInfoList


    def get_partInfo(self, cid, strucId, refaff, referer):
        url = 'http://service.citroen.com/docpr{}/affiche.do'.format(self._suffix)
        params = "?{}={}&{}={}&{}={}".format("ref", strucId, "refaff", refaff, "idFct", cid)
        url = url+params
        self.resTools.headers = {
            "Host": "service.citroen.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": referer,
        }
        responseInfo = self.resTools.parseUrl(url=url, fileName="affiche"+cid+strucId)
        imgPath = re.findall('var imgPath="(.*?)"', responseInfo.get("content"))
        imgPath = imgPath[0] if imgPath else ""
        if self.brand == "citroen" and imgPath:
            imgUrl = 'http://service.citroen.com/docprAC/{}'.format(imgPath)
        elif self.brand == "peugeot" and imgPath:
            imgUrl = 'http://service.citroen.com/docprAP/{}'.format(imgPath)
        else:
            imgUrl = ''
        return imgUrl


    def getImg(self, cid, groupId, strucId, refaff, imgUrl):
        cidPath = self.imgBasePath + os.sep + cid
        if not os.path.exists(cidPath):
            os.makedirs(cidPath)
        suffix = imgUrl[-4:]
        imgPath = cidPath + os.sep + strucId + suffix
        if not os.path.exists(imgPath):
            print('请求图片', cid, strucId, imgUrl)
            self.resTools.headers = {
                "Host": "service.citroen.com",
                "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
                "Accept": "image/webp,image/apng,image/*,*/*;q=0.8",
                "Referer": "http://service.citroen.com/docpr{}/affiche.do?ref={}&refaff={}&idFct={}".format(self._suffix, strucId, refaff, groupId),
            }
            imgRes = self.resTools.getImg(url=imgUrl)
            if imgRes:
                print("success")
                with open(imgPath, 'wb') as i:
                    i.write(imgRes.content)

    def get_subgroupImg(self, cid, subList, referer):
        for subInfo in subList:
            subImg = subInfo.get("subImg")
            subId = subInfo.get("subId")
            imgUrl = 'http://service.citroen.com/docpr{}/resources/4.28.7/{}/image/fonction/{}'.format(self._suffix, self._suffix, subImg)
            cidPath = self.imgBasePath + os.sep + cid
            if not os.path.exists(cidPath):
                os.makedirs(cidPath)
            suffix = imgUrl[-4:]
            imgPath = cidPath + os.sep + subId + suffix
            if not os.path.exists(imgPath):
                print('请求子组图片', cid, subId, imgUrl)
                self.resTools.headers = {
                    "Host": "service.citroen.com",
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
                    "Accept": "image/webp,image/apng,image/*,*/*;q=0.8",
                    "Referer": referer,
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                }
                imgRes = self.resTools.getImg(url=imgUrl)
                if imgRes:
                    print("success")
                    with open(imgPath, 'wb') as i:
                        i.write(imgRes.content)

    def _getData(self, cid, groupInfoList, referer):
        for groupInfo in groupInfoList:
            groupName = groupInfo.get("groupName")
            print('主组', groupName)
            groupId = groupInfo.get("groupId")
            subList = groupInfo.get("subList")

            # TODO 获取子组图片
            self.get_subgroupImg(cid, subList, referer)

            for subInfo in subList:
                subName = subInfo.get("subName")
                grisee = subInfo.get("grisee")
                print('子组', subName, grisee)
                if grisee:
                    print(subName, "灰色无数据")
                    continue
                subId = subInfo.get("subId")
                subImg = subInfo.get("subImg")
                strucInfoList = self.get_strucInfo(cid, groupId, subId, referer)
                for strucInfo in strucInfoList:
                    strucId = strucInfo.get("strucId")
                    strucName = strucInfo.get("strucName")
                    strucModel = strucInfo.get("strucModel")
                    print('分组', strucName, strucModel)
                    refaff = strucInfo.get("refaff")
                    imgUrl = self.get_partInfo(cid, strucId, refaff, referer)
                    if imgUrl:
                        self.getImg(cid, groupId, strucId, refaff, imgUrl)


    def run(self):
        cookies = self.login(cp_username, cp_password)
        self.resTools.cookies=cookies
        carList = self.get_carList()
        num = 0
        for car in carList:
            cid = car.get("cid")
            print("车型", car.get("name"), cid)
            self.resTools.basePath = self.basePath + os.sep + cid
            if not os.path.exists(self.resTools.basePath):
                os.makedirs(self.resTools.basePath)
            else :
                continue
            self.cid = cid
            vehCom, cat = cid.split("_")[0], cid.split("_")[1]
            for i in range(4):
                if i == 0:
                    self.ajaxRequestGetVehCom(vehCom, cat)
                    self.ajaxRequestGetVehCom(vehCom, cat, lcdv=True)
                    referer, menuList, groupInfoList = self.get_carIndex(vehCom, cat)
                    time.sleep(2)
                    self._getData(cid, groupInfoList, referer)
                    continue
                menu = menuList[i]
                referer, groupInfoList = self.get_groupInfoList(cid, menu, referer)
                self._getData(cid, groupInfoList, referer)

            self.new_search(referer)


def main():
    brand = "peugeot"  # citroen
    tools = CitroenAll(brand=brand)
    tools.run()

if __name__ == '__main__':
    main()